﻿using System;
using HtmlAgilityPack;
using ResourceCollectionDao;
using ResourceCollectionDomain.Entity;
using SpiderCore;
using SpiderCore.Ants;
using SpiderCore.Spiders;
using SpiderCore.Utility;

namespace ResourceCollectionSys.Spider
{
    /// <summary>
    /// 当当网蚂蚁
    /// </summary>
    public class BookuuAnt : Ant
    {
        public override void StartToWork(PageData pageData)
        {
            try
            {
                CurrentUrl = pageData.PageUrl;
                bool success = false;
                if (pageData.Data != null && pageData.Data.ToString() != string.Empty)
                {
                    switch (pageData.Flag)
                    {
                        case "book":
                            success = ParserThePage(pageData.Data.ToString(), pageData.PageUrl);
                            break;

                    }
                    if (success)
                    {
                        AntCaptain.SuccessFinishedTaskCount += 1;
                    }
                }
            }
            catch (Exception ex)
            {
                SpiderCaptain.GetInstance().StopToWork();
                AntCaptain.GetInstance().StopToWork(ex.ToString());
                CustomsLog.WriteExceptionErrorLog("博库蚂蚁工作异常", ex, "requst:" + pageData.PageUrl + ";content:" + pageData.Data.ToString());
            }
        }
        /// <summary>
        /// 解析博库页面
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        private bool ParserThePage(string html, string url)
        {
            try
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);

                Book v = new Book();
                var name = doc.GetElementbyId("name");
                if (name != null && !string.IsNullOrWhiteSpace(name.InnerText))
                {
                    string navalu = name.InnerText;
                    v.Name = navalu.Trim();
                    //内容
                    var content = doc.DocumentNode.SelectSingleNode("//div[@class='txt-wrap']");
                    if (content != null)
                    {
                        v.Description = content.InnerText;
                        //图片
                        //如果用child.SelectSingleNode("//*[@class=\"titlelnk\"]").InnerText这样的方式查询，是永远以整个document为基准来查询，
                        //这点就不好，理应以当前child节点的html为基准才对。
                        var img = content.SelectSingleNode("//div[@class='pic']//img[1]");
                        if (img != null)
                        {
                            v.DefaultImgUrl = img.Attributes["src"].Value;
                        }
                        v.CreateOn = DateTime.Now;
                        v.Url = url;
                        return BookDao.SaveToBookInfo(v);

                    }
                }

                return false;

            }
            catch (Exception ex)
            {
                throw new Exception("解析博库页面错误", ex);
            }

        }
    }
}
